/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package de.fhdw.hbs.client.ws;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.ws.BindingProvider;

import de.fhdw.hbs.client.generated.BewertungDTO;
import de.fhdw.hbs.client.generated.HbsImplService;
import de.fhdw.hbs.client.generated.HbsService;
import de.fhdw.hbs.client.generated.HbsServiceException_Exception;
import de.fhdw.hbs.client.generated.Hotel;
import de.fhdw.hbs.client.generated.HotelListDTO;
import de.fhdw.hbs.client.generated.HotelSucheDTO;
import de.fhdw.hbs.client.generated.KundeDTO;
import de.fhdw.hbs.client.generated.LoginDTO;
import de.fhdw.hbs.client.generated.ReservierungDTO;
import de.fhdw.hbs.client.generated.ReservierungListDTO;

/**
 * 
 * @author SZAHEL
 *
 */
public class HbsClient {

	// Attribute
	public static HbsService _port = null;
	static MessageDigest md = null;
	static String pwAlgorithm = "SHA";

	// Konstruktur

	public static HbsService getPortInstance() {
		if (_port == null) {
			_port = new HbsImplService().getHbsImplPort();
			log((BindingProvider) _port);
			try {
				md = MessageDigest.getInstance(pwAlgorithm);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return _port;
	}

	// Methoden
	public LoginDTO loginByEmail(	String email,
									String pw) throws HbsServiceException_Exception {

		try {
			md = MessageDigest.getInstance(pwAlgorithm);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		byte[] hash = md.digest(pw.getBytes());

		LoginDTO login = getPortInstance().loginByEmail(email, hash);

		if (login.getUserId() > 0 && login.getSessionId() > 0) {
			System.out.println("Login succesful! Your userId is " + login.getUserId() + " and you session id is "
					+ login.getSessionId() + "!");
		} else {
			System.out.println("Login failed!");
		}

		return login;
	}

	public KundeDTO getKundeById(int id) throws HbsServiceException_Exception {

		KundeDTO kunde = getPortInstance().getKundeById(id);

		return kunde;
	}

	public void addHotelBewertung(	Hotel h,
									int ausstattung,
									int lage,
									int sauberkeit,
									int service,
									int unterhaltung,
									int zimmer) throws HbsServiceException_Exception {

		BewertungDTO bewertung = new BewertungDTO();
		bewertung.setAusstattung(ausstattung);
		bewertung.setLage(lage);
		bewertung.setSauberkeit(sauberkeit);
		bewertung.setService(service);
		bewertung.setUnterhaltung(unterhaltung);
		bewertung.setZimmer(zimmer);

		getPortInstance().addHotelBewertung(h.getId(), bewertung);
	}
	
	public ReservierungListDTO getReservierungByKunde (KundeDTO k) {
		ReservierungListDTO reservierungen = new ReservierungListDTO();
		reservierungen = getPortInstance().getReservierungByKunde(k);
		return reservierungen;
	}

	public void addHotelReservierung(	Hotel h,
										KundeDTO k,
										Calendar von,
										Calendar bis,
										int anzahl_dz,
										int anzahl_ez,
										int anzahlPersonen) throws HbsServiceException_Exception {

		ReservierungDTO reservierung = new ReservierungDTO();
		reservierung.setKunde(k);
		reservierung.setHotel(h);

		try {
			reservierung.setAnreise(de.fhdw.hbs.client.utils.Mapper.mapCalendar2XML(von));
			reservierung.setAbreise(de.fhdw.hbs.client.utils.Mapper.mapCalendar2XML(bis));
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		reservierung.setAnzahlDz(anzahl_dz);
		reservierung.setAnzahlEz(anzahl_ez);
		reservierung.setPersonenzahl(anzahlPersonen);
		
		getPortInstance().addReservierung(reservierung);
	}

	public HotelListDTO getHotelByKriterien(HotelSucheDTO kriterien) throws HbsServiceException_Exception {

		HotelListDTO dtoList = getPortInstance().getHotelByKriterien(kriterien);

		return dtoList;
	}

	private static final void log(BindingProvider port) {
		if (Boolean.getBoolean("wsmonitor")) {
			String address = (String) port.getRequestContext().get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
			address = address.replaceFirst("8080", "4040");
			port.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
			port.getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
		}
	}

}
